package com.bjpowernode.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bjpowernode.entity.PmsAttr;
import com.bjpowernode.entity.PmsBrand;
import com.bjpowernode.entity.PmsProduct;
import com.bjpowernode.feign.FileService;
import com.bjpowernode.service.PmsAttrService;
import com.bjpowernode.service.PmsBrandService;
import com.bjpowernode.service.PmsCategoryService;
import com.bjpowernode.service.PmsProductService;
import com.bjpowernode.vo.ResultJson;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author zs
 * @since 2024-06-25
 */
@RestController
@RequestMapping("/pmsProduct")
public class PmsProductController {
    @Resource
    PmsBrandService pmsBrandService;
    @Resource
    PmsCategoryService pmsCategoryService;
    @Resource
    PmsAttrService pmsAttrService;
    @Resource
    FileService fileService;
    @Resource
    PmsProductService pmsProductService;

    //查询获取品牌和分类，用于商品基本信息的获取
    @GetMapping("/getData")
    public ResultJson<Map<String, List>> getData() {
        Map<String, List> map = new HashMap<>();
        //查询出启用的品牌
        map.put("brands", pmsBrandService.getActive());
        map.put("categories", pmsCategoryService.getAll());
        return ResultJson.success(map);
    }

    //级联选择器选择时查询获取商品属性
    @GetMapping("/getAttr")
    public ResultJson<List<PmsAttr>> getAttr(String[] categoryIds) {
        Map<String, List> map = new HashMap<>();
        return ResultJson.success(pmsAttrService.list(categoryIds));
    }

    //调用文件上传模块，上传商品详情页图片
    @PostMapping("/upload")
    public ResultJson<String> upload(MultipartFile file) throws Exception {
        return ResultJson.success(fileService.imageUpload(file));
    }

    //商品基本信息验证，保存。完成商品基本信息的添加
    @PostMapping("/save")
    public ResultJson<Boolean> save(String name, BigDecimal price, String brandId, String[] categoryId, MultipartFile[] files, String[] spu, String[] sku, String description, String[] skuArray) throws IOException {
        return ResultJson.success(pmsProductService.save(name, price, brandId, categoryId, files, spu, sku, description, skuArray), "添加商品成功");
    }

    //商品列表，进行分页查询
    @GetMapping("/list")
    public ResultJson<IPage> list(Integer pageNo, Integer pageSize) {
        return ResultJson.success(pmsProductService.list(pageNo, pageSize));
    }

    //改变商品状态
    @PostMapping("/changeStatus")
    public ResultJson<Boolean> changeStatus(String id, Boolean isPublish) {
        return ResultJson.success(pmsProductService.changeStatus(id, isPublish), isPublish ? "上架成功" : "下架成功");
    }

    //修改商品信息-获取品牌信息
    @GetMapping("/getBrands")
    public ResultJson<List<PmsBrand>> getBrands() {
        return ResultJson.success(pmsBrandService.getActive());
    }

    //修改商品信息-获取商品信息
    @GetMapping("/getById")
    public ResultJson<PmsProduct> getById(String id) {
        return ResultJson.success(pmsProductService.getById(id));
    }

    //修改商品信息-保存修改商品信息1
    @PostMapping("/updateBase")
    public ResultJson<Boolean> updateBase(String id, String name, BigDecimal price, String brandId) {
        return ResultJson.success(pmsProductService.updateBase(id, name, price, brandId), "修该基本信息成功");
    }

    //修改商品信息-保存修改商品信息2（spu）
    @PostMapping("/updateSpu")
    public ResultJson<Boolean> updateSpu(String id, String[] spu) {
        return ResultJson.success(pmsProductService.updateSpu(id, spu), "更新商品属性spu成功");
    }

    //修改商品信息-保存修改商品信息3（商品图片相册）
    @PostMapping("/updateAlbum")
    public ResultJson<Boolean> updateAlbum(String id, String[] names, MultipartFile[] files) throws IOException {
        return ResultJson.success(pmsProductService.updateAlbum(id, names, files), "更新商品相册成功");
    }

    //修改商品信息-保存修改商品信息4（商品图片详情）
    @PostMapping("/updateDescription")
    public ResultJson<Boolean> updateDescription(String id, String description) throws IOException {
        return ResultJson.success(pmsProductService.updateDescription(id, description), "更新商品详情成功");
    }
}
